package org.example.tree;

import java.awt.*;

/**
 * @ClassName  圆形阻挡物类，包含一个矩形表示其边界和一个圆表示其精确形状
 * @Description TODO
 * @Author hongjun
 * @Date 2024/5/17 下午10:48
 * @Version 1.0
 */
class Circle {
    int centerX, centerY, radius;

    public Circle(int centerX, int centerY, int radius) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.radius = radius;
    }

    // 判断点是否在圆内
    public boolean contains(int x, int y) {
        return (x - centerX) * (x - centerX) + (y - centerY) * (y - centerY) <= radius * radius;
    }

    // 判断矩形是否与圆相交
    public boolean intersects(Rectangle rect) {
        // 使用矩形到圆心的最近距离与半径比较
        int closestX = Math.max(centerX - rect.width / 2, Math.min(centerX + rect.width / 2, rect.x));
        int closestY = Math.max(centerY - rect.height / 2, Math.min(centerY + rect.height / 2, rect.y));
        int distanceX = centerX - closestX;
        int distanceY = centerY - closestY;
        return distanceX * distanceX + distanceY * distanceY <= radius * radius;
    }
    // 省略其他可能的方法...
}
